package thread;

public class SyncDemo1 {
    public static void main(String[] args) {
        Shop shop = new Shop();
        Shop2 shop2 = new Shop2();
        Thread thread = new Thread("小李"){
            public void run() {
                shop.buy();
            }
        };

        Thread thread1 = new Thread("小王"){
            public void run() {
                shop2.buy();
            }
        };
        thread.start();
        thread1.start();

    }
}


class Shop{
    public  void buy(){
        Thread t = Thread.currentThread();
        try {

            System.out.println(t.getName() + "正在挑衣服");
            Thread.sleep(3000);
            synchronized (this){
                System.out.println(t.getName()+"正在试衣服");
                Thread.sleep(3000);
            }
            System.out.println(t.getName()+"结账离开");
        } catch (InterruptedException e) {}

    }
}

class Shop2{
    public  void buy(){
        Thread t = Thread.currentThread();
        try {

            System.out.println(t.getName() + "正在挑衣服");
            Thread.sleep(3000);
            synchronized (this){
                System.out.println(t.getName()+"正在试衣服");
                Thread.sleep(3000);
            }
            System.out.println(t.getName()+"结账离开");
        } catch (InterruptedException e) {}

    }
}